/*
 * Copyright (c) 2022 Kirky
 * Licensed under the Open Software Licence, Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://opensource.org/licenses/osl-3.0.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lingeringdream.luciole;

import com.lingeringdream.luciole.helper.PreferencesHelper;
import com.lingeringdream.luciole.helper.ResourceHelper;
import com.lingeringdream.luciole.helper.generalDialog.GeneralButtonElement;
import com.lingeringdream.luciole.helper.generalDialog.GeneralDialogHelper;
import com.lingeringdream.luciole.model.Permissions;
import com.lingeringdream.luciole.slice.IdentitySlice;
import com.lingeringdream.luciole.widget.controller.FormController;
import com.lingeringdream.luciole.widget.controller.FormControllerManager;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.utils.Color;
import ohos.agp.window.service.Window;
import ohos.bundle.IBundleManager;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

/**
 * 识别 ability
 *
 * @author Kirky
 * @version 1.0
 * @date 2022/4/30
 */
public class IdentityAbility extends Ability {
    private static final int INVALID_FORM_ID = -1;
    private static PreferencesHelper preferencesHelper;
    private final HiLogLabel TAG = new HiLogLabel(HiLog.LOG_APP, 0x20, IdentityAbility.class.getSimpleName());

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setMainRoute(IdentitySlice.class.getName());
        preferencesHelper = PreferencesHelper.getInstance(getContext());
        setSystemColor();
        if (intentFromWidget(intent)) {
            String topWidgetSlice = getRoutePageSlice(intent);
            if (topWidgetSlice != null) {
                setMainRoute(topWidgetSlice);
            }
        }
//        addActionRoute("action.personInfo", PersonInfoSlice.class.getName());
//        addActionRoute("action.message", MessageSlice.class.getName());
//        addActionRoute("action.achievement", AchievementSlice.class.getName());
//        addActionRoute("action.aboutUs", AboutUsSlice.class.getName());


        verifyPermission(Permissions.CAMERA, Permissions.READ_MEDIA);
    }

    private void setSystemColor() {
        Window window = getWindow();

        window.setStatusBarColor(Color.TRANSPARENT.getValue());
        window.setNavigationBarColor(Color.TRANSPARENT.getValue());
    }

    private boolean intentFromWidget(Intent intent) {
        long formId = intent.getLongParam(AbilitySlice.PARAM_FORM_IDENTITY_KEY, INVALID_FORM_ID);
        return formId != INVALID_FORM_ID;
    }

    private String getRoutePageSlice(Intent intent) {
        long formId = intent.getLongParam(AbilitySlice.PARAM_FORM_IDENTITY_KEY, INVALID_FORM_ID);
        if (formId == INVALID_FORM_ID) {
            return null;
        }
        FormControllerManager formControllerManager = FormControllerManager.getInstance(this);
        FormController formController = formControllerManager.getController(formId);
        if (formController == null) {
            return null;
        }
        Class<? extends AbilitySlice> clazz = formController.getRoutePageSlice(intent);
        if (clazz == null) {
            return null;
        }
        return clazz.getName();
    }

    private void verifyPermission(Permissions... permissions) {
        for (Permissions permission : permissions) {
            if (verifySelfPermission(permission.getPerStr()) != IBundleManager.PERMISSION_GRANTED) {
                if (canRequestPermission(permission.getPerStr())) {
//                Apply for pop-up authorization (for the first time, or the user does not select prohibit and will not be prompted)
                    requestPermissionsFromUser(permission.getPerStrs(), permission.getResCode());
                } else {
//                Display the reason why the application needs permission and prompt the user to enter the setting authorization
                    new GeneralDialogHelper.Builder(getContext())
                            .setTitle(ResourceHelper.getString(getContext(), ResourceTable.String_failed_permission, "Failed to obtain permission", null))
                            .setContent(ResourceHelper.getString(getContext(), permission.getReason(), permission.getPerName(), null))
                            .addButton(ResourceHelper.getString(getContext(), ResourceTable.String_Delegating, "Delegating", null),
                                    GeneralButtonElement.TYPE_ACCENT, null)
                            .addButton(ResourceHelper.getString(getContext(), ResourceTable.String_cancel, "Cancel", null),
                                    GeneralButtonElement.TYPE_NORMAL, null)
                            .create().show();
                }
            }
            else {
//                权限已经授予
                preferencesHelper.putSync(permission.getPerName(), true);
            }
        }
    }
}
